//
//  BillViewController.m
//  work
//
//  Created by 徐宏飞 on 16/6/1.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "BillViewController.h"
#import "BillTableViewCell.h"
#import "BillModel.h"


static NSString *BillTableViewCell_ID = @"BillTableViewCell";


@interface BillViewController ()<UITableViewDelegate, UITableViewDataSource, NetworkToolerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *billTableView;
@property (nonatomic, strong) NSMutableArray *billListArray;  //项目列表
@property (nonatomic, assign) NSInteger loadpage;               //页码
@property (nonatomic, strong) ZhuangxiuManager *manager;
@property (nonatomic, strong) NetworkTooler *network;
@property (nonatomic, assign) NSInteger total;               //列表总个数
@property (nonatomic, strong) UIImageView *nullBgImage;      //数据图


@end

@implementation BillViewController


#pragma mark - life cycle
- (void)initNavigationBar
{
    self.enableReturnButton = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"账单";
    self.billListArray = [NSMutableArray array];
    _network = [NetworkTooler new];
    self.manager = [ZhuangxiuManager shareManager];
    _loadpage = 1;
    
    
    
    [_billTableView registerNib:[UINib nibWithNibName:@"BillTableViewCell" bundle:nil] forCellReuseIdentifier:BillTableViewCell_ID];
    _billTableView.tableFooterView = [UIView new];
    _billTableView.allowsSelection = NO;
    
    
    [self.view addSubview:self.nullBgImage];
    
    [self _postbillListDataWithPage:_loadpage];
    
    
    __weak BillViewController *weakself = self;
    
    _billTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //        self.page = 0;
        _loadpage = 1;
        [weakself updateNewData];
    }];
    
    _billTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakself updateMoreData];
    }];
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"MoneyList"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"MoneyList"];
}

#pragma mark - Data 
- (void) _postbillListDataWithPage:(NSInteger)page{
    NSString *pageStr = [NSString stringWithFormat:@"%ld", page];
    _network.delegate = self;
    NSDictionary *parameters = @{
                                 @"session_id":self.manager.sessionid,
                                 @"page":pageStr,
                                 @"rows":[NSString stringWithFormat:@"%d",listConut]
                                 };
//    NSLog(@"上传参数%@", parameters);
    [_network parameters:parameters withURL:getBankCashList_show  withIsPaparameters:YES];
    
}


#pragma mark - NetworkToolerDelegate
- (void)returnData:(id)data{
    [self endRefresh];
    self.bgView.hidden = YES;
    
    
    if (isNSDictionary) {
        
        if (_loadpage == 1) {
            [self.billListArray removeAllObjects];
        }
        
        NSArray *array = [data objectForKey:@"data"];
        _total =[[data objectForKey:@"total"] integerValue];
        for (NSMutableDictionary *dic in array) {
            BillModel *model = [BillModel modelWithDictionary:dic];
            [self.billListArray addObject:model];
        }
        
        if (self.billListArray.count == 0) {
            self.nullBgImage.hidden = NO;
        } else {
            self.nullBgImage.hidden = YES;
        }
        
        
        [self.billTableView reloadData];
    }
    
    
    
}
- (void)returnRequestError:(NSError *)error{
    [self endRefresh];
}
- (void)returnMsg:(NSString *)Msg{
    self.bgView.hidden = NO;
     [self endRefresh];
    [self.billListArray removeAllObjects];
    [_billTableView reloadData];
    [self showAlertWithTitle:@"温馨提示" message:Msg cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
}






#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.billListArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BillModel *model = self.billListArray[indexPath.row];
    
    BillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BillTableViewCell_ID];
    
    cell.model = model;
    
    return cell;
}










#pragma mark - CustomDelegate
#pragma mark - event response



#pragma mark - private methods
-(void)updateNewData{
    
    _loadpage = 1;
    [self _postbillListDataWithPage:_loadpage];
    
    
}


-(void)updateMoreData{
    
    _loadpage++;
    
    if (_loadpage <= (int)((_total/listConut)+1)) {
        
        [self _postbillListDataWithPage:_loadpage];
    }else{
        
        [_billTableView.mj_footer endRefreshingWithNoMoreData];
        
    }
    
}
-(void)endRefresh{
    [_billTableView.mj_header endRefreshing];
    [_billTableView.mj_footer endRefreshing];
}




#pragma mark - gettsers and setters
- (UIImageView *)nullBgImage{
    if (!_nullBgImage) {
        _nullBgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
        _nullBgImage.center = CGPointMake(SCREEN_WIDTH*0.5, (SCREEN_HEIGHT - 64)*0.5);
        _nullBgImage.image = [UIImage imageNamed:@"暂无数据"];
        _nullBgImage.hidden = YES;
    }
    return _nullBgImage;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
